package com.shundr.common;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;

import com.actionbarsherlock.app.ActionBar;
import com.shundr.R;
import com.shundr.base.BaseActivity;
import com.shundr.common.adapter.AreaAdapter;
import com.shundr.common.dao.AreaDao;
import com.shundr.common.dao.HistoryAreaDao;
import com.shundr.common.model.AreaInfo;
import com.shundr.common.model.HistoryAreaInfo;
import com.shundr.common.util.DateUtil;
import com.shundr.common.util.JsonUtil;
import com.shundr.common.util.ProgressDialogHelper;
import com.shundr.frame.util.LLogger;
import com.shundr.frame.util.StringUtil;

/**
 * 选择城市
 * AreaActivity
 * 
 * @author larry 2015-7-16 下午2:50:23 
 * @version 1.0.0
 *
 */
public class AreaActivity extends BaseActivity {
	
	private List<AreaInfo> mList = new ArrayList<AreaInfo>();
	
	private List<AreaInfo> mHistoryList = new ArrayList<AreaInfo>();
	private AreaAdapter mHistoryAdapter;
	
	private List<AreaInfo> mHotList = new ArrayList<AreaInfo>();
	private AreaAdapter mHotAdapter;
	
	private AreaDao mDao;
	private HistoryAreaDao mHistoryDao;
	
	private EditText etInput;
	private Button btnClear;
	
	private GridView gvSearch;
	private List<AreaInfo> mSearchList = new ArrayList<AreaInfo>();
	private AreaAdapter mSearchAdapter;
	
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 100:
				break;

			default:
				break;
			}
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initActionBar();
		setContentView(R.layout.activity_area);
		initView();
		mDao = new AreaDao();
		mHistoryDao = new HistoryAreaDao();
		
		ProgressDialogHelper.show(mContext);
		mHandler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				mList.addAll(mDao.getAllAreas());
//				mDao.initOrName(mList);
				List<HistoryAreaInfo> histories = mHistoryDao.getHistoryAreas();
				for(HistoryAreaInfo info : histories){
					mHistoryList.add(new AreaInfo(info.getName(), info.getCode()));
				}
				mHistoryAdapter.notifyDataSetChanged();
				
				mHotList.addAll(mDao.getAreasByParent("00"));
				mHotAdapter.notifyDataSetChanged();
				ProgressDialogHelper.dismiss();
			}
		}, 100);
	}
	
	private void initActionBar(){
		mActionBar.setCustomView(R.layout.actionbar_edittext);  
		mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
		mActionBar.setHomeButtonEnabled(true);
		mActionBar.setDisplayShowTitleEnabled(true);// 隐藏title
		// 隐藏Home logo
		mActionBar.setDisplayShowHomeEnabled(false);
		mActionBar.setDisplayHomeAsUpEnabled(true);
		
		etInput = (EditText)mActionBar.getCustomView().findViewById(R.id.et_input);
		btnClear = (Button)mActionBar.getCustomView().findViewById(R.id.btn_clear);
		btnClear.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				etInput.setText("");
			}
		});
		
		etInput.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				if(s.length() > 0){
					mSearchList.clear();
					String input = etInput.getText().toString();
					LLogger.e("larry", "是不是汉字啊  :  "+StringUtil.isChineseChar(input));
					LLogger.e("larry", "是不是字母啊  :  "+StringUtil.isEnglishLetter(input));
					if(StringUtil.isChineseChar(input)){//是汉字
						for (AreaInfo info : mList) {
							if (info.getName().substring(0, input.length()).contains(input)) {
								mSearchList.add(info);
							}
						}
					}else if(StringUtil.isEnglishLetter(input)){//是a-z
						
					}else{
						
					}
					mSearchAdapter.notifyDataSetChanged();
					gvSearch.setVisibility(View.VISIBLE);
					btnClear.setVisibility(View.VISIBLE);
				}else{
					gvSearch.setVisibility(View.GONE);
					btnClear.setVisibility(View.GONE);
				}
			}
		});
	}
	
	private void initView(){
		mHistoryAdapter = new AreaAdapter(mContext, mHistoryList);
		GridView gvHistory = (GridView) findViewById(R.id.grid_history);
		gvHistory.setAdapter(mHistoryAdapter);
		gvHistory.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				List<AreaInfo> list = new ArrayList<AreaInfo>();
				list.add(mHistoryList.get(position));
				updateHistory(mHistoryList.get(position));
				Intent intent = new Intent();
				intent.putExtra("json", JsonUtil.toJson(list).toString());
				setResult(502,intent);
				finish();
			}
		});
		
		mHotAdapter = new AreaAdapter(mContext, mHotList);
		GridView gridView = (GridView) findViewById(R.id.grid_hot);
		gridView.setAdapter(mHotAdapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				List<AreaInfo> list = new ArrayList<AreaInfo>();
				list.add(mHotList.get(position));
				updateHistory(mHotList.get(position));
				Intent intent = new Intent();
				intent.putExtra("json", JsonUtil.toJson(list).toString());
				setResult(502,intent);
				finish();
			}
		});
		
		mSearchAdapter = new AreaAdapter(mContext, mSearchList);
		gvSearch = (GridView) findViewById(R.id.grid_search);
		gvSearch.setAdapter(mSearchAdapter);
		gvSearch.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				List<AreaInfo> list = new ArrayList<AreaInfo>();
				list.add(mSearchList.get(position));
				updateHistory(mSearchList.get(position));
				Intent intent = new Intent();
				intent.putExtra("json", JsonUtil.toJson(list).toString());
				setResult(502,intent);
				finish();
			}
		});
	}
	
	private void updateHistory(AreaInfo info){
		HistoryAreaInfo history = new HistoryAreaInfo(info.getName(), info
				.getCode(), info.getParentCode(), info.getAreaCode());
		history.setTime(DateUtil.getNowTime());
		mHistoryDao.insert(history);
	}
	
}
